﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using RemoteObject;

namespace RemoteClient.UI
{
    public partial class DJobListView : UserControl
    {
        private DJobItem preSelectedItem;
        private SegmentListView segmentList;
        private System.Windows.Forms.Timer updateTimer;

        public SegmentListView SegmentList
        {
            get
            {
                return segmentList;
            }
            set
            {
                segmentList = value;
            }

        }
        public DJobListView()
        {
            InitializeComponent();
            //jobTalbe = new Hashtable();
            InitTimer();
            updateTimer.Start();
        }

        public void AddJob(DownloadJob dJob)
        {
            DJobItem newItem = new DJobItem(dJob);
            dJobList.Items.Add(newItem);
            refreshSegmentsList();
        }

        public void RemoveJob(DownloadJob dJob)
        {
            IEnumerator itemEnumerator = dJobList.Items.GetEnumerator();
            DJobItem jobItem = null;

            while (itemEnumerator.MoveNext())
            {
                DJobItem item = (DJobItem)itemEnumerator.Current;
                if (item.DJob.Equals(dJob))
                {
                    jobItem = item;
                    break;
                }
            }

            if (jobItem != null)
            {
                RemoveJobItem(jobItem);        
            }            
        }

        private void RemoveJobItem(DJobItem jobItem)
        {
            segmentList.RemoveAllSegments();
            jobItem.Dispose();
            dJobList.Items.Remove(jobItem);
        }

        public void StartDownload()
        {
            if (dJobList.SelectedItems.Count > 0)
            {
                IEnumerator enumerator = dJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DJobItem jobItem = (DJobItem)enumerator.Current;
                    Thread thread = new Thread(new ThreadStart(jobItem.DJob.Start));
                    thread.Start();                    
                }
            }                        
        }

        public void StopDownload()
        {
            if (dJobList.SelectedItems.Count > 0)
            {
                IEnumerator enumerator = dJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DJobItem jobItem = (DJobItem)enumerator.Current;
                    Thread thread = new Thread(new ThreadStart(jobItem.DJob.Cancel));
                    thread.Start();
                }
            }            
        }        

        private void InitTimer()
        {
            updateTimer = new System.Windows.Forms.Timer();
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Interval = 2000;
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            RefreshJobsInfo();
        }

        private void RefreshJobsInfo()
        {
            if (dJobList.SelectedItems.Count > 0)
            { 
                preSelectedItem = (DJobItem)dJobList.SelectedItems[0];
            }

            foreach (DJobItem item in dJobList.Items)
            {
                item.RefreshJobInfo();                
            } 
           
            refreshSegmentsList();

            preSelectedItem = null;
        }

        private List<SegmentItem> GetSegments(DownloadJob job)
        {
            Console.WriteLine(job.GetWorkerList().Count);

            List<SegmentItem> segments = new List<SegmentItem>();
            foreach (DownloadWorkder seg in job.GetWorkerList())
            {
                segments.Add(new SegmentItem(seg));
            }
            return segments;
        }

        private void refreshSegmentsList(DJobItem item)
        {
            segmentList.RemoveAllSegments();
            segmentList.AddSegments(GetSegments(item.DJob));
        }

        private void refreshSegmentsList()
        {
            if (dJobList.SelectedItems.Count > 0)
            {
                DJobItem dJobItem = (DJobItem)dJobList.SelectedItems[0];
                refreshSegmentsList(dJobItem);
            }
            else if (dJobList.Items.Count == 1)
            {
                refreshSegmentsList((DJobItem)dJobList.Items[0]);
            }
            else if (preSelectedItem != null)
            {
                refreshSegmentsList(preSelectedItem);
            }
            else
            {
                segmentList.RemoveAllSegments();
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dJobList.SelectedItems.Count > 0)
            {
                IEnumerator enumerator = dJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DJobItem jobItem = (DJobItem)enumerator.Current;
                    RemoveJobItem(jobItem);
                }
            }
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartDownload();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopDownload();
        }

        private void dJobList_SelectedIndexChanged(object sender, EventArgs e)
        {            
            refreshSegmentsList();
        }        
    }
}
